package com.itheima.genericit;

import java.util.Iterator;

public class MyArrayList<E> {

    private E[] array;
    private int size;
    private static final int DEFAULT_CAPACITY = 10;
    public MyArrayList() {
        array = (E[]) new Object[DEFAULT_CAPACITY];
    }
    public void add(E e) {
        if(size == array.length) {
            E[] newArray = (E[]) new Object[array.length * 2];
            System.arraycopy(array, 0, newArray, 0, array.length);
            array = newArray;
        }
        array[size] = e;
        size++;
    }
    public E get(int index) {
        if(index < 0 || index >= size) {
            throw new RuntimeException("越界了");
        }
        return array[index];
    }
    public int size() {
        return size;
    }
    public void set(int index, E e) {
        if(index < 0 || index >= size) {
            throw new RuntimeException("越界了");
        }
        array[index] = e;
    }
    public void remove(int index) {
        if(index < 0 || index >= size) {
            throw new RuntimeException("越界了");
        }
    }
    public void clear() {
        array = (E[]) new Object[DEFAULT_CAPACITY];
        size = 0;
    }
    public boolean contains(E e) {
        for(int i = 0; i < size; i++) {
            if(array[i].equals(e)) {
                return true;
            }
        }
        return false;
    }
}
